<template>
  <div class="container">
    <div class="app-container">
      <el-row class="row_add_btn">
        <el-button class="add_btn" type="primary" @click="addRole">添加角色</el-button>
      </el-row>
      <el-table v-loading="loading" :data="list">
        <el-table-column prop="name" label="角色" align="center">
          <template v-slot="{row}">
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <input v-else v-model="row.editRow.name" type="text">
          </template>
        </el-table-column>
        <el-table-column prop="state" label="启用" align="center">
          <template v-slot="{ row }">
            <span v-if="!row.isEdit"> {{ row.state === 1 ? "已启用" : "禁用" }} </span>
            <el-switch v-else v-model="row.editRow.state" :active-value="1" :inactive-value="0" size="mini" />
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center">
          <template v-slot="{row}">
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <input v-else v-model="row.editRow.description" type="text">
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{row}">
            <template v-if="!row.isEdit">
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>
              <el-popconfirm title="确定要删除该数据吗" @onConfirm="confirmDel(row.id)">
                <template #reference>
                  <el-button style="margin-left: 10px" size="mini" type="text">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
            <template v-else>
              <el-button size="mini" type="primary" @click="edit_ok(row)">确定</el-button>
              <el-button size="mini" @click="btn_cancel(row)">取消</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="height:80px" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager, next, jumper, total"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog title="新增角色" width="500px" :visible="showDialog" @close="close">
      <el-form ref="addForm" label-width="120px" :rules="formRules" :model="addForm">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addForm.name" style="width:300px" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="addForm.state"
            size="mini"
            inactive-text="启用"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addForm.description" style="width:300px" size="mini" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row style="width:100%;" type="flex" justify="center">
          <el-button type="primary" size="mini" style="margin-right:20px" @click="btnOk">确认</el-button>
          <el-button size="mini" type="text" style="color: #727479" @click="close">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { addRoleAPI, delRoleAPI, getRoleListAPI, updateRoleAPI } from '@/api/role'

export default {
  name: 'Role',
  data() {
    return {
      showDialog: false,
      loading: false,
      list: [],
      pageParams: {
        total: null,
        page: 1,
        pagesize: 5
      },
      addForm: {
        name: '',
        state: 1,
        description: ''
      },
      formRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleListAPI(this.pageParams)
      this.list = rows
      // 添加的动态属性 不具备响应式特点 需要使用$set
      this.list.forEach(el => {
        this.$set(el, 'isEdit', false)
        // 添加缓存数据,并初始化
        this.$set(el, 'editRow', {
          name: el.name,
          state: el.state,
          description: el.description
        })
      })
      this.pageParams.total = total
      this.loading = false
    },
    changePage(newPage) {
      this.pageParams.page = newPage
      this.getRoleList()
    },
    close() {
      // el组件的重置表单
      this.$refs.addForm.resetFields()
      // 清空addForm的值
      this.addForm = {
        name: '',
        description: '',
        state: 1
      }
      this.showDialog = false
    },
    addRole() {
      this.showDialog = true
    },
    async btnOk() {
      await this.$refs.addForm.validate()
      await addRoleAPI(this.addForm)
      this.close()
      this.getRoleList()
    },
    edit(row) {
      row.isEdit = true
      // 更新缓存数据
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    btn_cancel(row) {
      row.isEdit = false
    },
    async edit_ok(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRoleAPI({ id: row.id, ...row.editRow })
        this.$message.success('更新成功')

        Object.assign(row, { ...row.editRow, isEdit: false })
        // Object.assign(原对象，新对象) => 把新对象合并到原对象上去
        // this.$nextTick(() => {
        //   row.isEdit = false
        // })
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    async confirmDel(id) {
      await delRoleAPI(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) this.pageParams.page--
      this.getRoleList()
    }
  }
}
</script>
<style>
  .row_add_btn{
    padding: 10px 20px;
  }
  .add_btn{
    width: 80px;
    height: 35px;
    text-align: center;
    padding: 0px;
  }
</style>
